package com.gamecauca.animal;

import java.util.Random;

import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.gamecauca.Animation;
import com.gamecauca.Assets;
import com.gamecauca.World;
import com.gamecauca.common.Constant;
import com.gamecauca.common.GameConfig;
import com.gamecauca.common.GameObject;
import com.gamecauca.screen.GameCauCa;
import com.gamecauca.screen.GameScreen;

public class Fish extends GameObject {
	private int iTimeToReset = 3;// bien random dung de reset lai huong chuyen dong
								// cua ca
//	public FishMoneys money = new FishMoneys();
	
	public boolean earnMoney = false;
	private float moneyX;
	private float moneyY;
	private float iTimer;
	
	private Animation fishAnimation;
	private Animation captureAnimation;
	private Animation noCaptureAnimation;
	private TextureRegion deadFish;
	private TextureRegion currentFrame;
	private int kind;// loai ca
	private int stateFish ;// trang thai cá,0: đang bơi,1:bị bắt giữ:2:mắc câu....
	private float fontMoneyScale = 0.6f;
	private int timeToFinish;
	private boolean earnBombMoney = false;
	
	public FishMoneys money = new FishMoneys();
	
	public Fish() {
	}

	public Fish(float x, float y, int kind, TextureRegion region, int sideAppear) {
		super(x, y, region.getRegionWidth(), region.getRegionHeight());
		initFish(kind, sideAppear);
	}

	public Fish(float x, float y, float width, float height, int kind,int sideAppear) {
		super(x, y, width, height);
		initFish(kind, sideAppear);
	}
	public void initFish(int kind,int sideAppear){
		this.kind = kind;
		this.sideAppear = sideAppear;
		TextureRegion t []= new TextureRegion[6];
		TextureRegion t1 []= new TextureRegion[3];
		TextureRegion t2 []= new TextureRegion[2];
		if (sideAppear == Constant.LEFTSIDE) {
			direction = Constant.RIGHT;
			for(int i = 0;i<t.length;i++){
				t[i] = Assets.fishRegionRight[kind][i];
			}
			fishAnimation = new Animation(0.25f,t);
			for(int i = 0;i<t1.length;i++){
				t1[i] = Assets.captureFishRegionRight[kind][i];
			}
			captureAnimation = new Animation(0.25f,t1);
			for(int i = 0;i<t2.length;i++){
				t2[i] = Assets.noCaptureFishRegionRight[kind][i];
			}
			noCaptureAnimation = new Animation(0.25f,t2);
		} else if (sideAppear == Constant.RIGHTSIDE) {
			direction = Constant.LEFT;
			for(int i = 0;i<6;i++){
				t[i] = Assets.fishRegionLeft[kind][i];
			}
			fishAnimation = new Animation(0.25f,t);
			for(int i = 0;i<t1.length;i++){
				t1[i] = Assets.captureFishRegionLeft[kind][i];
			}
			captureAnimation = new Animation(0.25f,t1);
			for(int i = 0;i<t2.length;i++){
				t2[i] = Assets.noCaptureFishRegionLeft[kind][i];
			}
			noCaptureAnimation = new Animation(0.25f,t2);
		}
		initialVxy(kind);
		iCounterToEraseResetDirectFlag = 0;
		isReset = false;
		inCollision = false;
		this.kindOfObject = Constant.FISH;
		weight = GameConfig.getFishWeight(kind + 1);
		moneyValue = GameConfig.getGiaTri(kind + 1);
		blood = GameConfig.getFishBlood(kind + 1);
		stateFish = 0;// ban dau la dang boi
		deadFish = Assets.deadFishRegion[kind];
	}
	public void initialVxy(int kind) {
		// set van toc di chuyen cua ca
		if (kind == 0) {
			x_Speed = GameCauCa.rand.nextInt(5) + 1;
		} else if (kind == 1) {
			x_Speed = GameCauCa.rand.nextInt(5) + 1.5f;
//			y_Speed = rand.nextInt(5) + 1;
		} else if (kind == 2) {
			x_Speed = GameCauCa.rand.nextInt(5) + 2;
//			y_Speed = rand.nextInt(5) + 1;
		} else if (kind == 3) {
			x_Speed = GameCauCa.rand.nextInt(5) + 2.5f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		} else if (kind == 4) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		} else if (kind == 5) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3.5f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		} else if (kind == 6) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3.6f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		} else if (kind == 7) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3.7f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		} else if (kind == 8) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3.8f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 9) {
			x_Speed = GameCauCa.rand.nextInt(5) + 3.9f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 10) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 11) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.1f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 12) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.2f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 13) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.3f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 14) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.4f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
		}
		else if (kind == 15) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.5f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
			// KIND TERIBLE
		}
		else if (kind == 16) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.6f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
			// KIND TERIBLE
		}
		else if (kind == 17) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.7f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
			// KIND TERIBLE
		}
		else if (kind == 18) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.7f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
			// KIND TERIBLE
		}
		else if (kind == 19) {
			x_Speed = GameCauCa.rand.nextInt(5) + 4.2f;
			// Y_Speed = GameScreen.rand.nextInt(5) + 1;
			// KIND TERIBLE
		}
		
	}
	public void render(SpriteBatch batch){
		
		stateTime += Gdx.graphics.getDeltaTime(); 
		switch (stateFish) {
		case Constant.FISH_DANGBOI:
			if(direction == Constant.LEFT ||direction == Constant.LEFT_DOWN ||direction == Constant.LEFT_UP){
				if (fishAnimation!= null )currentFrame = fishAnimation.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
			}
			else if(direction == Constant.RIGHT ||direction == Constant.RIGHT_DOWN ||direction == Constant.RIGHT_UP){
				if (fishAnimation!= null ) currentFrame = fishAnimation.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
			}
			if (currentFrame!= null ) batch.draw(currentFrame, position.x, position.y);
			break;
		case Constant.FISH_KEOLEN:

			if(!earnMoney){
				if (captureAnimation!= null ) currentFrame = captureAnimation.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				if (currentFrame!= null ) batch.draw(currentFrame, position.x, position.y);
			}
			else{
				
				addMoneySign(batch);		
			}
			
			break;
		case Constant.FISH_GIATCAU:
			if(direction == Constant.LEFT ||direction == Constant.LEFT_DOWN ||direction == Constant.LEFT_UP){
				if (noCaptureAnimation!= null )currentFrame = noCaptureAnimation.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
			}
			else if(direction == Constant.RIGHT ||direction == Constant.RIGHT_DOWN ||direction == Constant.RIGHT_UP){
				if (noCaptureAnimation!= null ) currentFrame = noCaptureAnimation.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
			}
			if (currentFrame!= null ) batch.draw(currentFrame, position.x, position.y);
			break;
		case Constant.FISH_DEAD:
			if(!earnBombMoney){
				
				currentFrame = deadFish;
				batch.draw(currentFrame, position.x, position.y);
			}
			else{
				
				addMoneySign(batch);
			}
			
			break;
		default:
			break;
		}
		
	}


	public void update() {
		
		switch (stateFish) {
		case Constant.FISH_DANGBOI:
			if (!this.checkCollision()) {
				if (direction == Constant.LEFT) {
					this.position.sub(x_Speed, 0);
				} else if (direction == Constant.LEFT_DOWN) {
					this.position.sub(x_Speed, y_Speed);
				} else if (direction == Constant.LEFT_UP) {
					this.position.sub(x_Speed, -1 * y_Speed);
				} else if (direction == Constant.RIGHT) {
					this.position.add(x_Speed, 0);
				} else if (direction == Constant.RIGHT_DOWN) {
					this.position.add(x_Speed, -1 * y_Speed);
				} else if (direction == Constant.RIGHT_DOWN) {
					this.position.add(x_Speed, y_Speed);
				}
				inCollision = false;
			}
			else {
				inCollision = true;
			}
			break;
		case Constant.FISH_KEOLEN: // ca chet vi cau
			x_Speed = 0;
			direction = Constant.UP;
			if(this.position.y < 240)//240 la muc nuoc
				this.position.add(0, y_Speed);
			else {
				timeToFinish++;
				
				if(timeToFinish % 20 == 0){
					
					earnMoney = true;
					timeToFinish = 0;
				}
									
			}
			break;
		case Constant.FISH_GIATCAU:
			x_Speed = 0;
			y_Speed = 0;
			iTimer++;
			if(iTimer>1000000) iTimer = 0;
			if(iTimer % 25 == 0)World.isCatchOk = false;// bật cờ sớm 1 tí trước khi cá thoát câu
			if(iTimer % 30 == 0) {
				stateFish = Constant.FISH_DANGBOI;
				this.direction = GameCauCa.rand.nextInt(2);
				this.initFish(kind, this.direction);
			}
			break;
		case Constant.FISH_DEAD: // ca chet vi bomb
			x_Speed = 0;
			y_Speed = 5;
			
			if(this.position.y <= 240){
				this.position.add(x_Speed, y_Speed);
			}
			
			break;
		default:
			break;
		}
	}

	// neu ca va cham voi bien trai | phai thi tra ve true,neu khong tra ve
	// false
	public boolean checkCollision() {
		if (this.position.x < 0 - 3*this.bounds.getWidth()
				|| this.position.x > Constant.SCREEN_WIDTH
						+ 3* this.bounds.getWidth()) {
			return true;
		}
		return false;
	}

	// reset lai huong di chuyen cua ca sau khi va cham voi bien
	@Override
	public void resetDirection() {
		if (isReset)
			return;
		if (direction == Constant.RIGHT) {
			int arrDirect[] = { Constant.LEFT, Constant.RIGHT_DOWN,
					Constant.RIGHT_UP };
			// chi chon 3 huong,co the them vao sau khi lam xong cac phan kia
			// neu dang di chuyen sang phai,gap bien thi no
			// random theo cac huong la Phai | Trai_di len |Trai_di xuong
			int randDirect = GameCauCa.rand.nextInt(3);
			direction = arrDirect[randDirect];
			return;
		} else if (direction == Constant.LEFT) {
			int arrDirect[] = { Constant.RIGHT, Constant.LEFT_DOWN,
					Constant.LEFT_UP };
			// neu dang di chuyen sang trai,gap bien thi no
			// random theo cac huong la Phai | Trai_di len |Trai_di xuong
			int randDirect = GameCauCa.rand.nextInt(3);
			direction = arrDirect[randDirect];
			return;
		} else if (direction == Constant.RIGHT_UP) {
			direction = Constant.RIGHT_DOWN;
			return;
		} else if (direction == Constant.RIGHT_DOWN) {
			direction = Constant.RIGHT_UP;
			return;
		} else if (direction == Constant.LEFT_UP) {
			direction = Constant.LEFT_DOWN;
			return;
		} else if (direction == Constant.LEFT_DOWN) {
			direction = Constant.LEFT_UP;
			return;
		}
	}
	
	public void addMoneySign(SpriteBatch batch){
		
		iTimer += 0.035f;
		
		if(iTimer > 1.25f){
			this.position.x = moneyX - 50;
			this.position.y = moneyY + 20;
			
			fontMoneyScale += (0.035f * 0.1f);
			Assets.collectMoneyFont.setScale(fontMoneyScale);
			Assets.collectMoneyFont.draw(batch,String.valueOf("+" + money.earnMoneyWithKind(this.kind)), moneyX, moneyY + 20);		
		}
		
		if(fontMoneyScale > 1.0f){
			
			GameScreen.totalMoney += moneyValue;
			this.isCaptureFinished = true;
			Assets.collectMoneyFont.setScale(0.6f);
		}
	}
	

	@Override
	public int getCoin() {
		return 0;
	}

	@Override
	public void Eat() {

	}

	public void setCaptureFinished() {
		isCaptureFinished = true;
	}
	public void setVxy(int x,int y) {
		this.x_Speed = x;
		this.y_Speed = y;
	}
	public void setAnimation(Animation ani){
		fishAnimation = ani;
	}
	public int getState(){
		return stateFish;
	}
	public void setState(int state){
		stateFish = state;
	}

	public void setMoneyX(float moneyX) {
		this.moneyX = moneyX;
	}

	public float getMoneyX() {
		return moneyX;
	}

	public void setMoneyY(float moneyY) {
		this.moneyY = moneyY;
	}

	public float getMoneyY() {
		return moneyY;
	}

	public void setKind(int ikind) {
		kind = ikind;
	}
	public int getKind() {
		return kind ;
	}

	public void setEarnBombMoney(boolean earnBombMoney) {
		this.earnBombMoney = earnBombMoney;
	}

	public boolean isEarnBombMoney() {
		return earnBombMoney;
	}

}
